// pages/inpram/cl/drugStatistics/drugStatistics.js
import { $wuxCalendar } from '../../../../dist/index'
const network = require('../../../../utils/network.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {

    value1: ['2020-01-01'],
    value2: ['2020-01-30'],
    // pram_startTime: '2020-01-01',
    // pram_endTime: '2020-01-30',

    //选择统计类型之后的参数
    statistic_select: false,
    pram_statistic: {
      statisticType: 1,
      statisticName: '药品消耗排名'
    },

    order_select: false,
    pram_order: {
      orderType: 1,
      orderName: '金额'
    },

    //统计类型列表
    statistic_list: [
      {
        statisticType: 1,
        statisticName: '药品消耗排名'
      },
      {
        statisticType: 2,
        statisticName: '国产药消耗排名'
      }, {
        statisticType: 3,
        statisticName: '进口药消耗排名'
      },
      {
        statisticType: 4,
        statisticName: '抗生素消耗排名'
      }
    ],
    order_list:[
      {
        orderType: 1,
        orderName: '金额'
      },
      {
        orderType: 2,
        orderName: '数量'
      }
    ]


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    //调用封装好的网络请求，获取数据
    // network.get('http://localhost:8081/utils/drugclass', {}
    // ).then(function (res) {
    //   console.log('res:', res)
    //   var obj1 = res.data.data

    //   that.setData({
    //     'drug_list': obj1,

    //   })
    // }).then(function (res) {

    // }).catch(function (res) {
    //   console.log(res)
    // })
  },

  // 开始时间选择  

  openCalendar1() {
    $wuxCalendar().open({
      value: this.data.value1,
      onChange: (values, displayValues) => {
        console.log('onChange', values, displayValues)
        this.setData({
          value1: displayValues,
        })
      },
    })
  },

  // 结束时间选择  
  openCalendar2() {
    $wuxCalendar().open({
      value: this.data.value2,
      // multiple: true,
      onChange: (values, displayValues) => {
        console.log('onChange', values, displayValues)
        this.setData({
          value2: displayValues,
        })
      },
    })
  },

  bindPickerChange_statistic: function (e) {
    let that = this;
    var nowData = [];
    nowData = that.properties.statistic_list;//当前option的数据是引入组件的页面传过来的，所以这里获取数据只有通过this.properties
    var nowIdx = e.target.dataset.index;//当前点击的索引
    // var nowText = nowData[nowIdx].text;//当前点击的内容
    // console.log('picker数组为', nowData);
     console.log('picker发送选择改变，携带值为', e.detail.value);
    that.setData({   //给变量赋值
      //dept_index: e.detail.value,  //每次选择了下拉列表的内容同时修改下标然后修改显示的内容，显示的内容和选择的内容一致
      statistic_select: true,
      'pram_statistic.statisticType': nowData[e.detail.value].statisticType,
      'pram_statistic.statisticName': nowData[e.detail.value].statisticName,

    })
    console.log('自定义值:', that.properties.pram_statistic);
  },


  bindPickerChange_order: function (e) {
    let that = this;
    var nowData = [];
    nowData = that.properties.order_list;//当前option的数据是引入组件的页面传过来的，所以这里获取数据只有通过this.properties
    var nowIdx = e.target.dataset.index;//当前点击的索引
    // var nowText = nowData[nowIdx].text;//当前点击的内容
    // console.log('picker数组为', nowData);
    console.log('picker发送选择改变，携带值为', e.detail.value);
    that.setData({   //给变量赋值
      //dept_index: e.detail.value,  //每次选择了下拉列表的内容同时修改下标然后修改显示的内容，显示的内容和选择的内容一致
      order_select: true,
      'pram_order.orderType': nowData[e.detail.value].orderType,
      'pram_order.orderName': nowData[e.detail.value].orderName,

    })
    console.log('自定义值:', that.properties.pram_order);
  },


  // bindPickerChange_statistic: function (e) {
  //   let that = this;
  //   var nowData = [];
  //   nowData = that.properties.statistic_list;//当前option的数据是引入组件的页面传过来的，所以这里获取数据只有通过this.properties
  //   var nowIdx = e.target.dataset.index;//当前点击的索引
  //   // var nowText = nowData[nowIdx].text;//当前点击的内容
  //   // console.log('picker数组为', nowData);
  //   console.log('picker发送选择改变，携带值为', e.detail.value);
  //   that.setData({   //给变量赋值
  //     //dept_index: e.detail.value,  //每次选择了下拉列表的内容同时修改下标然后修改显示的内容，显示的内容和选择的内容一致
  //     statistic_select: true,
  //     'pram_statistic.statisticType': nowData[e.detail.value].statisticType,
  //     'pram_statistic.statisticName': nowData[e.detail.value].statisticName,

  //   })
  //   console.log('自定义值:', that.properties.pram_statistic);
  // },

  // 时间比较函数
  compareDate: function (startDate, endDate) {
    var arrStart = startDate.split("-");
    var startTime = new Date(arrStart[0], arrStart[1], arrStart[2]);
    var startTimes = startTime.getTime();
    var arrEnd = endDate.split("-");
    var endTime = new Date(arrEnd[0], arrEnd[1], arrEnd[2]);
    var endTimes = endTime.getTime();
    if (endTimes <= startTimes) {
      return false;
    }
    return true;
  },
  /**
   * 检查参数是否合法，跳转
   */
  determine: function () {
    var that = this
    var day = that.compareDate(that.data.value1[0], that.data.value2[0]);
    console.log('day', day)
    if (!day) {
      wx.showModal({

        title: '提示',

        content: '开始时间必须小于结束时间一天',

      })

    } else {
    wx.redirectTo({
      url: '../../../view/cl/drugStatistics/drugStatistics?startTime=' + this.data.value1[0] + '&endTime=' + this.data.value2[0] + '&statisticType=' + that.data.pram_statistic.statisticType + '&order=' + that.data.pram_order.orderType
    })
    }
  },

  cancel: function () {
    wx.navigateBack({ changed: true });
  }
})